package com.bocchi.demo1;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpServletRequest;

@WebListener
public class MyListener implements ServletRequestListener{
    long startTime = System.currentTimeMillis();
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("hspRequestListener 监听到 request对象创建");

        ServletRequest servletRequest = sre.getServletRequest();
        System.out.println("记录访问日志");
        System.out.println("访问IP="+servletRequest.getRemoteAddr());
        System.out.println("访问的资源="+((HttpServletRequest)servletRequest).getRequestURL());
        System.out.println("请求方法="+((HttpServletRequest) servletRequest).getMethod());
        System.out.println("请求URI="+((HttpServletRequest) servletRequest).getRequestURI());
        System.out.println("查询字符串="+((HttpServletRequest) servletRequest).getQueryString());

    }
    public void requestDestroyed(ServletRequestEvent sre) {
        long endTime = System.currentTimeMillis();
        System.out.println("hspRequestListener 监听到 request对象被销毁");
        long executionTime = endTime - startTime;
        System.out.println("请求时间为"+executionTime+"毫秒");
    }

}